package com.code.generator.builder;

import java.io.IOException;
import java.util.Map;

import com.code.generator.ExceputionUtils;
import com.code.generator.GeneratorUtils;
import com.code.generator.entity.TableEntity;
import com.code.plugin.MessagesProperties;
import com.code.view.entity.HandlerVO;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;


/**
 * service,Dao和controller生成类
 *
 *
 * @author 朱国军
 * @version 2015年2月9日 朱国军
 */
public class ClassBuilder extends BaseBuilder {

    private void createClass(TableEntity tableEntity, Configuration cfg, Map<String, Object> tableData, String classSuffix,
            String packageNmae, String ftlName, String outPutPath,String packagePath) throws IOException, TemplateException {
        /* 获取或创建一个模版 */
        Template temp = cfg.getTemplate(ftlName, "UTF-8");
        String filePath = outPutPath + GeneratorUtils.getFilePath(packagePath) + packageNmae + "/" + tableEntity.getClassName() + classSuffix + ".java";
        GeneratorUtils.witeFile(filePath, temp, tableData);
    }


    @Override
    public boolean builder(TableEntity tableEntity, String packagePath, HandlerVO handlerVO, Configuration cfg) {
        Map<String, Object> tableData = getDataMap(tableEntity, packagePath, handlerVO);
        String outPutPath = handlerVO.getOutPutPath();
        try {
            if(handlerVO.getImplButton().getSelection()){
                createClass(tableEntity, cfg, tableData, "Service", "service", "Service_impl.ftl", outPutPath,packagePath);
                createClass(tableEntity, cfg, tableData, "ServiceImpl", "service/impl", "ServiceImpl_impl.ftl", outPutPath,packagePath);
                createClass(tableEntity, cfg, tableData, "Dao", "dao", "Dao_impl.ftl", outPutPath,packagePath);
                if(handlerVO.getMyBatis().getSelection()){
                    createClass(tableEntity, cfg, tableData, "DaoImpl", "dao/impl", "DaoImpl_mybatis_impl.ftl", outPutPath,packagePath);
                }else{
                    createClass(tableEntity, cfg, tableData, "DaoImpl", "dao/impl", "DaoImpl_ibatis_impl.ftl", outPutPath,packagePath);
                }
            }else{
                createClass(tableEntity, cfg, tableData, "Service", "service", "Service.ftl", outPutPath,packagePath);
                if(handlerVO.getMyBatis().getSelection()){
                    createClass(tableEntity, cfg, tableData, "Dao", "dao", "Dao_mybatis.ftl", outPutPath,packagePath);
                }else{
                    createClass(tableEntity, cfg, tableData, "Dao", "dao", "Dao_ibatis.ftl", outPutPath,packagePath);
                }
            }
            
        } catch (IOException e) {
            ExceputionUtils.logging(MessagesProperties.log_class_builder_io_exception, e);
            return false;
        } catch (TemplateException e) {
            ExceputionUtils.logging(MessagesProperties.log_class_builder_template_exception, e);
            return false;
        } catch (Exception e) {
            ExceputionUtils.logging(MessagesProperties.log_class_builder_other_exception, e);
            return false;
        }
        return true;
    }
}
